package com.example.registerfunction;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.Serializable;
import java.util.Map;
import java.util.Set;

public class RegisterActivity extends AppCompatActivity {

    Map<String,String> receive;
    EditText edit_user;
    EditText edit_password;
    EditText edit_password1;
    Button button_register;
    TextView notice_words;
    TextView return_out;
    Boolean isSameUser = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Intent intent = this.getIntent();
        receive = (Map<String, String>) intent.getSerializableExtra("id");
        edit_user = (EditText) findViewById(R.id.user);
        edit_password = (EditText) findViewById(R.id.password);
        edit_password1 = (EditText) findViewById(R.id.password1);
        button_register = (Button) findViewById(R.id.bt_register);
        notice_words = (TextView) findViewById(R.id.notice_words);
        return_out = (TextView) findViewById(R.id.return_to_activity);
        button_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                notice_words.setTextColor(Color.parseColor("#FFFF0000"));
                if (edit_password.getText().toString() != edit_password1.getText().toString()) {
                    notice_words.setText("两次密码不相同！请重新输入！");
                    edit_password.setText("");
                    edit_password1.setText("");
                } else {
                    Set keySet = receive.keySet();
                    for (Object key : keySet) {
                        if (key.equals(edit_user.getText().toString())) {
                            isSameUser = true;
                        }
                    }
                    if (isSameUser == true) {
                        notice_words.setText("用户名已注册！");
                        edit_user.setText("");
                        edit_password.setText("");
                        edit_password1.setText("");
                        isSameUser = false;
                    } else {
                        notice_words.setTextColor(Color.parseColor("#FF008000"));
                        notice_words.setText("恭喜！用户创建成功！");
                        receive.put(edit_user.getText().toString(),edit_password.getText().toString());
                        edit_user.setText("");
                        edit_password.setText("");
                        edit_password1.setText("");
                    }
                }
            }
        });
        return_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(RegisterActivity.this,login.class);
                intent1.putExtra("id",(Serializable)receive);
                startActivity(intent1);
            }
        });
    }

    //每次传入的Intent都会刷新
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }
}